import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'
Vue.prototype.$http=axios;
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name:"home",
    redirect:"/login",
    meta:{
        auth:false,
    }
  },
  {//新闻页面
    path: '/news',
    name:"news",
    component:()=>import("../views/DexCom.vue"),
    meta:{
        auth:true,
    },
    children:[{
      name: "addnews",
      path: "addnews",
      component: ()=>import("../views/AddNews.vue"),
      meta:{
        auth:true,
    },
      },
      {
        path: 'newslist',
        name:"newslist",
        component:()=>import("../views/NewList.vue"),
        meta:{
          auth:true,
      },
        },
         {//新闻detail
        path: 'detail/:id',
        name:"detail",
        component:()=>import("../views/DeTail.vue"),
         meta:{
        auth:true,
    }
  },
  ]
  },
  // {//新闻newpage
  //   path: '/newslist',
  //   name:"newslist",
  //   component:()=>import("../views/NewListPage.vue"),
  //   meta:{
  //       auth:true,
  //   }
  // },
  {
    path: '/login',
    name: 'login',
    component:()=>import("../views/MyLogin.vue"),
    meta:{
      auth:false,
  }
  },
  // {
  //   path: '/cart',
  //   name: 'cart',
  //   component:()=>import("../views/MyCart.vue"),
  //   meta:{
  //     auth:true,
  // }
  // },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes 
})

router.beforeEach(function(to,from,next){
  // console.log(to,from);
  if(to.meta.auth){
    let login = localStorage.getItem("login")
    // console.log(login);
    if(login){
      next()
    }else{
      next("/login")
    }
  }else{
    next()
  }
  
})
export default router
